package com.dhu.service.impl;

import com.dhu.dao.BarMapper;
import com.dhu.dao.PostMapper;
import com.dhu.dao.UserFollowMapper;
import com.dhu.pojo.Post;
import com.dhu.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.Watchable;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private BarMapper barMapper;
    @Autowired
    private UserFollowMapper userFollowMapper;

    @Override
    public Post selectPostById(int id) {
        try {
            return postMapper.selectPostById(id);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<Post> selectHomePostList(int num) {
        try {
            return postMapper.selectHomePostList(num);
        } catch (Exception e) {
            return null;
        }
    }

    // 获取关注用户的帖子
    @Override
    public List<Post> selectByFollowId(int id) {
        try {
            int[] ids = userFollowMapper.selectFollowUser(id);
            return postMapper.selectByFollowId(ids);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<Post> selectPostByBarId(int barId) {
        try {
            return postMapper.selectPostByBarId(barId);
        } catch (Exception e) {
            return null;
        }
    }

    // 根据用户id获取帖子
    @Override
    public List<Post> selectByUserId(int userId) {
        try {
            return postMapper.selectByUserId(userId);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public int createPost(Post post) {
        try {
            // 吧帖子数+1
            barMapper.addPostNum(post.getBarId());
            return postMapper.createPost(post);
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public int createImgPost(Post post) {
        try {
            // 吧帖子数+1
            barMapper.addPostNum(post.getBarId());
            return postMapper.createImgPost(post);
        } catch (Exception e) {
            return -1;
        }
    }
}
